from django.shortcuts import render_to_response, get_object_or_404
from django.http import HttpResponseRedirect
from django.template import RequestContext

from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User

from obols.models import *
from obols.forms import *
from projects.forms import Project, Task

def obol_list(request,
                 group_slug=None, group_slug_field=None, group_qs=None,
                 extra_context=None,
                 *args, **kw):
    
    #todo: these were args in wiki
    #template_dir does not work here
    template_name='obols.html'
    template_dir='obols'
    
    group = Project.objects.get(slug=group_slug)
    
    req_agent = ObolAgent.objects.get_or_create(request.user, request.user.username)
    otg = ObolsToGive.objects.get_or_create(req_agent, group).obols
        
    if request.method == 'POST':
        event_form = ObolTaskForm(group, request.POST)
        print 'event_form:', event_form
        if event_form.is_valid():
            oe = event_form.save(commit=False)
            oe.group = group
            oe.from_whom=req_agent
            to_whom = ObolAgent.objects.get(pk=request['to_whom'])
            oe.to_whom = to_whom
            if request['task']:
                task = Task.objects.get(pk=request['task'])
                oe.what_resource = task
            oe.save()
            return HttpResponseRedirect('/%s/%s/%s/'
               % ('projects', group_slug, 'obols'))
    else:
        # todo: shd use group_slug_field and group_qs
        # but tnen this only works for projects now anyway...

        
        template_params = {'group_slug': group_slug,
                   'group_slug_field': group_slug_field,
                   'group': group,
                   'obols_to_give': otg}
        
        #if group_slug is not None:
        #    template_params['group'] = group
        if extra_context is not None:
            template_params.update(extra_context)
            
        obol_list = ObolEvent.objects.agent_scores_for_group(group)
        template_params['obol_list'] = obol_list
            
        if otg:
            event_form = ObolTaskForm(group)
            template_params['event_form'] = event_form

        return render_to_response('/'.join([template_dir, template_name]),
                                  template_params,
                                  context_instance=RequestContext(request))


def member_obols(request,
                 obol_agent,
                 group_slug=None, group_slug_field=None, group_qs=None,
                 extra_context=None,
                 *args, **kw):

    the_agent = get_object_or_404(ObolAgent, name=obol_agent)
    print 'group_slug:', group_slug
    group = get_object_or_404(Project, slug=group_slug)
    
    template_name='member_obols.html'
    template_dir='obols'

    obol_events = ObolEvent.objects.events_for_agent_in_group(the_agent, group)
    template_params = {'group_slug': group_slug,
                   'group_slug_field': group_slug_field,
                   'group': group,
                   'obol_agent': the_agent,
                   'obol_events': obol_events}
    
    return render_to_response('/'.join([template_dir, template_name]),
                                  template_params,
                                  context_instance=RequestContext(request))
    